//author:jxx
import store from "@/store/index";
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
let extension = {
  components: {//动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: () => import("./distributor/product.vue"),//{ template: "<div>扩展组xx件</div>" },
    gridBody: '',
    gridFooter: '',
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  buttons: { view: [], box: [], detail: [] },//扩展的按钮
  methods: {//事件扩展
    getOption (field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        })
      })
      return option;
    },
    onInit () {
      //新建窗口的宽和高
      this.boxOptions.width = 1000;
      this.boxOptions.height = 600;
      //    单选、不显示复选框、显示序号
      this.textInline = true;
      let channelType = this.getOption("channelType");
      let spare10 = this.getOption("spare10");
      let smsId = this.getOption("smsId");
      let supplierCode = this.getOption("supplierCode");
      smsId.onChange = (val, option) => {
        spare10.hidden = false;
        let data = option.data.filter(item => item.key == val);
        if (data.length > 0) { this.editFormFields[spare10.field] = data[0].remark; } else { spare10.hidden = true }
      }
      //   添加操作列
      this.columns.push({
        title: '操作',
        fixed: 'right',
        width: 150,
        render: (h, { row, column, index }) => {
          if (row.distributorType == "2005") {
            return h(
              "div", { style: {} }, [
              h(
                "a", {
                props: {},
                style: { "margin-left": "10px" },
                on: {
                  click: (e) => {
                    this.$refs.gridHeader.open(row)
                  }
                }
              },
                "产品发布"
              ),
            ])
          }
        }
      })

      channelType.onChange = (v, n) => {
        console.log(v)

        let newArr = channelType.data.filter(j => {
          return j.key == v
        })[0].remark.split(',')
        sessionStorage.setItem("remarkarr", newArr);
        newArr.unshift('distributorType', 'channelType', 'distributorName', 'timeContainer', 'verificationmethod', 'supplierCode')
        let i = 0
        this.editFormOptions.forEach(o => {
          o.forEach(p => {
            p.hidden = true
          })
        })
        while (newArr[i]) {
          for (let k = 0; k < this.editFormOptions.length; k++) {
            for (let lk = 0; lk < this.editFormOptions[k].length; lk++) {
              if (this.editFormOptions[k][lk].field == newArr[i]) {
                this.editFormOptions[k][lk].hidden = false
                continue;
              }
            }
          }
          i++
        }
        if (v == 'FXTX') {
          supplierCode.hidden = false
        } else {
          supplierCode.hidden = true
        }
      }
    },
    searchBefore (param) { //查询ViewGird表数据前,param查询参数
      //添加其他查询条件,也可以在后台扩展中写查询条件
      let wheres = {
        'name': 'distributorType',
        'value': '2005',
        'displayType': 'select'
      }
      param.wheres.push(wheres)
      return true;
    },
    rowClick ({ row, column, event }) { //查询界面table点击行选中当前行
      this.$refs.table.$refs.table.toggleRowSelection(row);
      return true;
    },
    direct () {
      let channelType = this.getOption("channelType");
      let distributorName = this.getOption("distributorName");
      let timeContainer = this.getOption("timeContainer");//起止时间
      let verificationmethod = this.getOption("verificationmethod");//起止时间
      channelType.hidden = false;
      channelType.required = true;
      distributorName.hidden = false;
      distributorName.required = true;
      timeContainer.hidden = false
      timeContainer.required = true;
      verificationmethod.hidden = false

    },
    notdirect () {
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field != "distributorType" && item.field != "creator" && item.field != "createDate" && item.field != "modifier" && item.field != "modifyDate" && item.field != "supplierCode") {
            item.hidden = true;
            item.required = false;
          }
        })
      })
    },

    async modelOpenBeforeAsync (row) {
      let distributorType = this.getOption("distributorType");
      if (row.distributorType == 2005) { this.direct(); } else { this.notdirect(); }
      distributorType.onChange = (val, option) => {
        if (val == 2005) {
          this.direct();
        } else {
          this.notdirect();
        }
      }
      return true;
    },
    // 点击编辑/新建按钮弹出框后
    async modelOpenAfter (row) {
      console.log(this.editFormOptions)
      let supplierCode = this.getOption("supplierCode");
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == 'distributorType') {
            item.data.forEach(kv => {
              if (kv.value == '直营') {
                kv.value = ''
                kv.hidden = true
                kv.disabled = true
                // console.log(kv)
              }
            })
          }
        })
      })

      if (this.currentAction == "update") {
        this.editFormFields.timeContainer = [row.distributorStartTime, row.distributorEndTime]
        if (row.channelParams) {
          let newArr = row.channelParams.split(',')
          console.log(newArr)
          sessionStorage.setItem("remarkarr", row.channelParams);
          newArr.unshift('distributorType', 'channelType', 'distributorName', 'timeContainer', 'verificationmethod', 'supplierCode')
          let i = 0
          this.editFormOptions.forEach(o => {
            o.forEach(p => {
              p.hidden = true
            })
          })
          while (newArr[i]) {
            for (let k = 0; k < this.editFormOptions.length; k++) {
              for (let lk = 0; lk < this.editFormOptions[k].length; lk++) {
                if (this.editFormOptions[k][lk].field == newArr[i]) {
                  console.log(this.editFormOptions[k][lk].field + ":" + newArr[i])
                  this.editFormOptions[k][lk].hidden = false
                  continue;
                }
              }
            }
            i++
          }
        }
        if (row.channelType == 'FXTX') {
          supplierCode.hidden = false
        } else {
          supplierCode.hidden = true
        }
      }
      let distributorType = this.getOption("distributorType");
      await this.http.post("/api/Dictionary/GetVueDictionary", ["distributor_Type"], true).then(res => {
        console.log(res, 'resss')
        if (res) {
          if (res.length > 0) {
            res[0].data.forEach(item => {
              if (item.value == "渠道") {
                this.$set(distributorType, "data", [item])
              }
            })
          }
        }
      })
      return true
    },
    addBefore (formData) { //新建保存前formData为对象，包括明细表
      // return console.log(formData);
      return this.veriFromTable(formData)
    },
    updateBefore (formData) { //编辑保存前formData为对象，包括明细表、删除行的Id
      // return console.log(formData);
      return this.veriFromTable(formData)
    },
    // 保存前编辑
    veriFromTable (formData) {
      // formData.detailData=[]
      formData.mainData.channelParams = sessionStorage.getItem(
        "remarkarr"
      );
      let timeContainer = formData.mainData.timeContainer.split(",")
      if (timeContainer.length > 0 && timeContainer[0] != "") {
        formData.mainData.distributorStartTime = timeContainer[0]
        formData.mainData.distributorEndTime = timeContainer[1]
      } else {
        this.$Message.error("请选择有效起止日期！")
      }
      return true
    }
  }
};
export default extension;
